package com.warmlight.voicepacket.ui.communitylm.widget;

import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatDialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.utils.Log;
import com.warmlight.voicepacket.utils.ShareUtils;
import com.warmlight.voicepacket.utils.ToastUtil;

import java.io.Serializable;
import java.util.Map;

/**
 * 分享按钮
 *
 */
public class CommunityShare extends AppCompatDialogFragment implements View.OnClickListener {

    public static CommunityShare communityShare;


    private String title;
    private String msc;
    private String url;
    private String img;
    private String type;
    private String id;
    private String is_mine;
    private OnDelete onDelete;

    public static CommunityShare getInstance(Map<String,String> map){
        communityShare=new CommunityShare();

        Bundle bundle = new Bundle();
        bundle.putSerializable("share", (Serializable) map);

        communityShare.setArguments(bundle);
        return communityShare;
    }

    public interface OnDelete{
        void onDelete();
    }

    public void setOnDelete(OnDelete onDelete){
        this.onDelete = onDelete;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Log.e("das");

        Bundle bundle = getArguments();
        if (bundle != null){
            Map<String,String> share = (Map<String, String>) bundle.getSerializable("share");
            title = share.get("title");
            msc = share.get("msc");
            url = share.get("url");
            img = share.get("img");
            type = share.get("type");
            id = share.get("id");

            //0 1
            is_mine = share.get("is_mine");

        }else{

        }
        Dialog dialog = new Dialog(getContext(), R.style.share_dialog);
        dialog.setContentView(R.layout.community_share);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        setCancelable(true);

        initView(dialog);

        Window window = dialog.getWindow();
        window.setWindowAnimations(R.style.dialogAnim3);
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.dimAmount = 0.2f;
        window.setAttributes(attributes);


        return dialog;
    }

    private void initView(Dialog dialog){
        LinearLayout ll_wx = dialog.findViewById(R.id.ll_wx);
        ll_wx.setOnClickListener(this);
        LinearLayout ll_wxcircle = dialog.findViewById(R.id.ll_wxcircle);
        ll_wxcircle.setOnClickListener(this);
        LinearLayout ll_qq = dialog.findViewById(R.id.ll_qq);
        ll_qq.setOnClickListener(this);
        LinearLayout ll_qzone = dialog.findViewById(R.id.ll_qzone);
        ll_qzone.setOnClickListener(this);
        LinearLayout ll_report = dialog.findViewById(R.id.ll_report);
        ll_report.setOnClickListener(this);
        LinearLayout ll_copy = dialog.findViewById(R.id.ll_copy);
        ll_copy.setOnClickListener(this);

        if (TextUtils.equals("1",is_mine)){
            ImageView iv_del = dialog.findViewById(R.id.iv_del);
            TextView tv_del = dialog.findViewById(R.id.tv_del);
            iv_del.setImageResource(R.drawable.share_del);
            tv_del.setText("删除");
        }
    }

    /**
     * 若帖子有文字正文：
     -卡片标题：显示文字正文
     -卡片摘要：显示“皮一下很开心，一起来玩皮~”
     -卡片图片：首张图片/视频封面图，纯文字则无图
     若帖子没有文字正文：
     -卡片标题：显示“这个帖子好有趣，给你也看看！”
     -卡片摘要：显示“皮一下很开心，一起来玩皮~”
     -卡片图片：首张图片/视频封面图，纯文字则无图
     * @param share_media
     */
    private void share(SHARE_MEDIA share_media){

        ShareUtils.shareWeb(getContext(), share_media, url, title, msc, img, new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {

            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {

            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {

            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_wx:
                share(SHARE_MEDIA.WEIXIN);
                dismiss();
                break;
            case R.id.ll_wxcircle:
                share(SHARE_MEDIA.WEIXIN_CIRCLE);
                dismiss();
                break;
            case R.id.ll_qq:
                share(SHARE_MEDIA.QQ);
                dismiss();
                break;
            case R.id.ll_qzone:
                share(SHARE_MEDIA.QZONE);
                dismiss();
                break;
            case R.id.ll_report:
                if (TextUtils.equals("1",is_mine)){
                    if (onDelete != null){
                        onDelete.onDelete();
                    }
                }else {
                    ReportDialog reportDialog = new ReportDialog();
                    reportDialog.setID(type, id);
                    reportDialog.show(getFragmentManager(), "report");
                }
                dismiss();
                break;
            case R.id.ll_copy:
                ClipboardManager clipboardManager = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clipData = ClipData.newPlainText("copy",url);
                clipboardManager.setPrimaryClip(clipData);
                ToastUtil.showShort(getContext(),"复制成功");
                dismiss();
                break;
        }
    }
}
